package com.jy.appmarket.fragment;

import com.jy.appmarket.R;
import com.jy.appmarket.activity.CheckVerifyCodeActivity;
import com.jy.appmarket.dialog.WaitDialog;
import com.jy.appmarket.network.engine.BaseNetEngine;
import com.jy.appmarket.network.engine.GetVerifyCodeNetEngine;
import com.jy.appmarket.network.resultdata.GetVerifyCodeResult;
import com.jy.appmarket.network.task.NetTask;
import com.jy.appmarket.network.task.NetTask.NetTaskListener;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.Toast;

public class FragmentRegister extends Fragment implements OnClickListener, NetTaskListener{
	private EditText mPhoneNumEdit;
	private WaitDialog mWaitDialog;
	private String mPhoneNum;
	private NetTask mNetTask;
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.fragment_register_layout, container, false);

		initViews(view);
		return view;
	}

	private void initViews(View v) {
		mPhoneNumEdit = v.findViewById(R.id.user_name_edit);
		v.findViewById(R.id.register_btn).setOnClickListener(this);
	}
	
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.register_btn:
			register();
			break;

		default:
			break;
		}
	}
	
	private void register() {
		mPhoneNum = mPhoneNumEdit.getText().toString().trim();

		if (TextUtils.isEmpty(mPhoneNum)) {
			Toast.makeText(getActivity(), R.string.please_input_username,
					Toast.LENGTH_SHORT).show();
			return;
		}
		if (mPhoneNum.length() != 11) {
			Toast.makeText(getActivity(), R.string.username_format_error,
					Toast.LENGTH_SHORT).show();
			return;
		}
		
		showWaitDialog();
		GetVerifyCodeNetEngine engine = new GetVerifyCodeNetEngine(mPhoneNum, "register");
		mNetTask = new NetTask(getActivity(), engine, 0);
		mNetTask.setListener(this);
		new Thread(mNetTask).start();
	}
	
	@Override
	public void onTaskRunSuccessful(int tag, BaseNetEngine engine) {
		dismissWaitDialog();
		GetVerifyCodeResult resultData = (GetVerifyCodeResult) engine.getResultData();
		if (!resultData.isOK()) {
			String message = resultData.getMessage();
			if (!TextUtils.isEmpty(message)) {
				Toast.makeText(getActivity(), message, Toast.LENGTH_SHORT).show();
			} else {
				Toast.makeText(getActivity(), R.string.get_verify_code_error, Toast.LENGTH_SHORT).show();
			}
		} else {
			Toast.makeText(getActivity(), R.string.get_verify_code_success, Toast.LENGTH_SHORT).show();
			Intent intent = new Intent(getActivity(), CheckVerifyCodeActivity.class);
			Bundle bundle = new Bundle();
			bundle.putString(CheckVerifyCodeActivity.EXTRA_NAME, "手机注册（1/3）");
			bundle.putString(CheckVerifyCodeActivity.EXTRA_TYPE, "register");
			bundle.putString(CheckVerifyCodeActivity.EXTRA_PHONE_NUM, mPhoneNum);
			bundle.putBoolean(CheckVerifyCodeActivity.EXTRA_NEED_GET_VERIFY_CODE, false);
			intent.putExtras(bundle);
			startActivity(intent);
			getActivity().finish();
		}
		
	}

	@Override
	public void onTaskRunError(int tag) {
		dismissWaitDialog();
		Toast.makeText(getActivity(), R.string.network_error, Toast.LENGTH_SHORT).show();
	}

	@Override
	public void onTaskRunCanceled(int tag) {
		dismissWaitDialog();
	}

	@Override
	public void onDestroy() {
		if (mNetTask != null) {
			mNetTask.cancel();
			mNetTask.setListener(null);
			mNetTask = null;
		}
		dismissWaitDialog();
		super.onDestroy();
	}
	
	private void showWaitDialog(){
		if (mWaitDialog == null) {
			mWaitDialog = new WaitDialog(getActivity());
			mWaitDialog.setCanceledOnTouchOutside(false);
			mWaitDialog.setCancelable(false);
		}
		if (mWaitDialog.isShowing()) {
			return;
		}
		mWaitDialog.showTitle("请稍候...").show();
	}
	
	private void dismissWaitDialog(){
		if (mWaitDialog != null) {
			mWaitDialog.dismiss();
		}
	}
}
